package org.apache.curator.framework;

import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.testng.annotations.Test;
import java.nio.charset.StandardCharsets;
public class CuratorDemo {
    @Test
    public void testBasic() throws Exception {
        ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);
        // The simplest way to get a CuratorFramework instance. This will use default values.
        // The only required arguments are the connection string and the retry policy
        CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.220.146:2181", retryPolicy);
        client.start();
        //client.create().forPath("/jianghuiwen", "jianghuiwen".getBytes());
        byte[] bs = client.getData().forPath("/jianghuiwen");
        System.out.println(new String(bs));
        // 怎样理解watcher是一次性的
        Watcher watcher = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                System.out.println("Receive watched event：" + event.getType());
            }
        };
        client.getData().usingWatcher(watcher).forPath("/jianghuiwen");
        client.setData().forPath("/jianghuiwen","newvalue".getBytes(StandardCharsets.UTF_8));
        client.delete().forPath("/jianghuiwen");
        client.create().forPath("/jianghuiwen", "新增节点".getBytes());
    }
}
